<?php
namespace Addons\Category\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
        $this->cate_type = dict("cate_type");
        $this->model = BoxModel('Addons://Category/AddonCategory');
    }

    /**
     * 分类列表
     * @return
     */
    public function index()
    {
        //$this->type=I("type","doc");
        $this->type = I('type');
        //dump($this->type);
        $this->data = $this->model->getCate($this->admin_store_id, $this->type);
        $this->admin_display();
    }

    /**
     * 分类
     */
    public function add()
    {
        $this->type = I('type');
        if ($id = I('id')) {
            $this->data = $this->model->find($id);
            $this->type = $this->data['type'];
        }
        $this->show_cate_data = $this->model->getCate($this->admin_store_id, $this->type);
        $this->admin_display();
    }

    /**
     * 分类
     * @return [type] [description]
     */
    public function del()
    {
        if ($id = I('id')) {
            $this->model->delete($id);
            $this->jump("删除成功");
        }
    }

    public function add_do()
    {
        if (IS_POST) {
            $model = $this->model;
            $data = $model->create();
            if ($cate_id = I('cate_id')) {
                $model->where(array('cate_id' => $cate_id))->save($data);
            } else {
                $data['store_id'] = $this->admin_store_id;
                $model->add($data);
            }
            $this->jump("操作成功");
        }
    }


    public function init($parm)
    {
        $this->param = $parm;
        $this->display();
    }

    public function show_select($parm)
    {
        $this->type = $parm['type'];
        $this->cate_id = intval($parm['pid']);
        $show_cate_data = $this->model->cateList($this->type, $this->cate_id);
        if (!$show_cate_data) {
            $show_cate_data = null;
        }
        echo json_encode($show_cate_data);
        // $this->display();
    }


    public function show_list($parm)
    {
        $this->type = $parm['type'];
        $this->cate_id = $parm['cate_id'];
        $this->show_cate_data = $this->model->getCate($this->type);
        return $this->show_cate_data;
    }


    public function get_parents($parm)
    {
        $this->cate_id = $parm['cate_id'];
        $data = $this->model->Getparents($this->cate_id);
        echo json_encode($data);
    }


}